package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.model.MultiData;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityShowMultiSportBinding;
import com.apex.sdk.databinding.ItemSportListBinding;
import com.apex.sdk.dialog.WaitingDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class ShowMultiSport extends AppCompatActivity {
    ActivityShowMultiSportBinding activityShowMultiSportBinding;
    private WaitingDialog waitingDialog;
    Thread mThread;
    List<MultiData> dataList;
    SimpleDateFormat simpleDateFormat;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x05) {
                if (waitingDialog!=null&&waitingDialog.isShowing()){
                    waitingDialog.dismiss();
                }
                if (dataList == null || dataList.isEmpty()) {
                    Toast.makeText(ShowMultiSport.this, getString(R.string.no_data), Toast.LENGTH_SHORT).show();
                    return;
                }
                ListAdapter listAdapter = new ListAdapter();
                activityShowMultiSportBinding.sportList.setAdapter(listAdapter);
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityShowMultiSportBinding = ActivityShowMultiSportBinding.inflate(getLayoutInflater());
        setContentView(activityShowMultiSportBinding.getRoot());
        activityShowMultiSportBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityShowMultiSportBinding.tool);
        getSupportActionBar().setTitle(null);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        activityShowMultiSportBinding.sportList.setLayoutManager(linearLayoutManager);
        activityShowMultiSportBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityShowMultiSportBinding.description.apexId.setText("apexId");
        activityShowMultiSportBinding.description.hlId.setText("hlId");
        activityShowMultiSportBinding.description.time.setText("time");
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(ShowMultiSport.this);
        }
        if (!waitingDialog.isShowing()) {
            waitingDialog.show();
        }
        mThread = new Thread() {
            @Override
            public void run() {
                super.run();
                dataList = EABleManager.getInstance().queryMultiData();
                if (dataList != null && !dataList.isEmpty()) {
                    Collections.sort(dataList, new Comparator<MultiData>() {
                        @Override
                        public int compare(MultiData o1, MultiData o2) {
                            if (o1 != null && o2 == null) {
                                long sTime = o1.getStartTime();
                                long oTime = o2.getStartTime();
                                if (sTime > oTime) {
                                    return -1;
                                } else if (sTime == oTime) {
                                    return 0;
                                } else {
                                    return 1;
                                }
                            }
                            return 0;
                        }
                    });
                }
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(0x05);
                }
            }
        };
        mThread.start();
    }

    class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHold> {
        ItemSportListBinding itemSportListBinding;

        @NonNull
        @Override
        public ListHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            itemSportListBinding = ItemSportListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ListHold(itemSportListBinding.getRoot());
        }

        @Override
        public void onBindViewHolder(@NonNull ListHold holder, int position) {
            if (dataList == null || dataList.isEmpty()) {
                return;
            }
            MultiData multiData = dataList.get(position);
            itemSportListBinding.apexId.setText(multiData.getType()+"");
            String hlId = Integer.toHexString(multiData.getHlSportType());
            itemSportListBinding.hlId.setText("0x"+(hlId.length() < 2 ? "0" + hlId : hlId));
            if (simpleDateFormat == null) {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            String time = simpleDateFormat.format(new Date(multiData.getStartTime()));
            itemSportListBinding.time.setText(time);

        }

        @Override
        public int getItemCount() {
            return dataList == null ? 0 : dataList.size();
        }

        class ListHold extends RecyclerView.ViewHolder {

            public ListHold(@NonNull View itemView) {
                super(itemView);
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (mThread!=null){
            mThread.interrupt();
            mThread=null;
        }
        if (waitingDialog!=null){
            waitingDialog.dismiss();
            waitingDialog=null;
        }
        simpleDateFormat=null;
        super.onDestroy();
    }
}
